//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"EmitterManager.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include "EmitterManager.h"
#include "../TinyXML/tinyxml.h"

#include "../../SGD Wrappers/SGD_Math.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/SGD_Util.h"
#include "../../SGD Wrappers/SGD_String.h"

using namespace std;

EmitterManager* EmitterManager::instance = nullptr;

EmitterManager* EmitterManager::GetInstance()
{
	if(!instance)
		instance = new EmitterManager;

	return instance;
}

EmitterManager::EmitterManager()
{

}

EmitterManager::~EmitterManager()
{

}



void	EmitterManager::LoadEmitter(string name)
{
	TiXmlDocument emitter;

	ostringstream filename;
	filename << "resource/Particles/" << name.c_str() << ".xml";

	emitter.LoadFile(filename.str().c_str());
	TiXmlElement* root = emitter.RootElement();

	TiXmlElement* e = root->FirstChildElement("Name");
	TCHAR LName[32];
	const char* eName = e->GetText();	
	CSTR_TO_TSTR(LName, 32, eName);

	e = root->FirstChildElement("Data");

	int loop;
	e->Attribute("IsLooping",&loop);
	bool looping = false;
	if(loop)
		looping = true;
	int sizeX, sizeY;
	e->Attribute("SizeX", &sizeX);
	e->Attribute("SizeY", &sizeY);
	int maxParticles;
	double emissionRate;
	e->Attribute("EmissionRate", &emissionRate);
	e->Attribute("MaxParticles", &maxParticles);
	int eType;
	e->Attribute("EmitterType", &eType);

	TiXmlElement* p = root->FirstChildElement("Particle");
	TiXmlElement* texture = p->FirstChildElement("Texture");
	const char* pFile = texture->GetText();
	ostringstream textureFile;
	textureFile <<"resource/Particles/" << pFile;
	TCHAR LFile[128];
	CSTR_TO_TSTR(LFile, 128, textureFile.str().c_str());

	double minLife, maxLife, minSpeed, maxSpeed;
	p->Attribute("MinLife", &minLife);
	p->Attribute("MaxLife", &maxLife);
	p->Attribute("MinSpeed", &minSpeed);
	p->Attribute("MaxSpeed", &maxSpeed);
	
	double emitDirX, emitDirY, minDir, maxDir;
	p->Attribute("EmitDirX", &emitDirX);
	p->Attribute("EmitDirY", &emitDirY);
	p->Attribute("MinDir", &minDir);
	p->Attribute("MaxDir", &maxDir);

	int minScaleX, minScaleY, maxScaleX, maxScaleY;
	p->Attribute("MinScaleX", &minScaleX);
	p->Attribute("MinScaleY", &minScaleY);
	p->Attribute("MaxScaleX", &maxScaleX);
	p->Attribute("MaxScaleY", &maxScaleY);

	double rotationRate;
	p->Attribute("RotationRate", &rotationRate);

	int startA, startR, startG, startB;
	p->Attribute("StartA", &startA);
	p->Attribute("StartR", &startR);
	p->Attribute("StartG", &startG);
	p->Attribute("StartB", &startB);

	int finA, finR, finG, finB;
	p->Attribute("FinA", &finA);
	p->Attribute("FinR", &finR);
	p->Attribute("FinG", &finG);
	p->Attribute("FinB", &finB);

	int partId = CSGD_TextureManager::GetInstance()->LoadTexture(LFile);

	Emitter* add = new Emitter(name,partId, EmitterType(eType), NULL, sizeX, sizeY, 0,0,maxParticles,(float)emissionRate,
		(float)minLife, (float)maxLife, (float)minSpeed, (float)maxSpeed, (float)emitDirX, (float)emitDirY, 
		(float)minDir, (float)maxDir, minScaleX, minScaleY, maxScaleX, maxScaleY, (float)rotationRate,
		D3DCOLOR_ARGB(startA, startR, startG, startB), D3DCOLOR_ARGB(finA, finR, finG, finB));

	allEmitters.push_back(add);


}

void	EmitterManager::UnloadEmitter(string name)
{
	for (unsigned int i = 0; i < allEmitters.size(); i++)
	{
		Emitter* temp = allEmitters[i];
		if(temp->GetName() == name)
		{
			if(temp->GetPlaying())
				StopEmitter(name);
			allEmitters.erase(allEmitters.begin()+i);
			delete temp;
			break;
		}

	}
}

void	EmitterManager::PlayEmitter(POINT pos, Entity* object, string name)
{
	for (unsigned int i = 0; i < allEmitters.size(); i++)
	{
		if(allEmitters[i]->GetName() == name)
		{
			allEmitters[i]->SetPlaying(true);
			allEmitters[i]->SetEmitPos(pos);
			allEmitters[i]->SetFollow(object);
			liveEmitters.push_back(allEmitters[i]);
			break;
		}

	}
}

void	EmitterManager::StopEmitter(string name)
{
	for (unsigned int i = 0; i < liveEmitters.size(); i++)
	{
		if(liveEmitters[i]->GetName() == name)
		{
			liveEmitters[i]->SetPlaying(false);
			liveEmitters.erase(liveEmitters.begin() + i);
			break;
		}

	}
}

void	EmitterManager::StopAll()
{
	for (unsigned int i = 0; i < liveEmitters.size(); i++)
	{
		liveEmitters[i]->SetPlaying(false);
		liveEmitters.erase(liveEmitters.begin() + i);
	}
}

void	EmitterManager::UpdateAll(float dt)
{
	for (unsigned int i = 0; i < liveEmitters.size(); i++)
	{
		liveEmitters[i]->Update(dt);
	}
}

void	EmitterManager::RenderAll()
{
	for (unsigned int i = 0; i < liveEmitters.size(); i++)
	{
		liveEmitters[i]->Render();
	}
}

